var rootPath = getRootPath();
var currentPagNo = 1;
$(function() {
	$('#userIntegralLogTable').DataTable({
        "language": {
        	"lengthMenu": "每页 _MENU_ 条记录",
            "zeroRecords": " ",
            "info": "当前 _START_ 条到 _END_ 条 &nbsp;&nbsp;&nbsp;共 _TOTAL_ 条记录",
            "infoEmpty": "无记录",
            "infoFiltered": "",//(从 _MAX_ 条记录过滤)
            "search": "搜索",
            "processing": "<font color='red'>载入中...</font>",
            "paginate": {
                "first": "首页",
                "previous": "上一页",
                "next": "下一页",
                "last": "尾页"
            }
        },
        serverSide: true,
        info: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        processing: true,
        searching: false,
        pagingType: "simple_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        stateSave: true,//刷新时是否保存状态
        autoWidth: true,//自动计算宽度
        orderMulti: true,
        pageLength: 10,
        fnInitComplete:function(){//创建完成回调缩略图创建
        	//$('.galpop').galpop();
        },
		"ajax" : {
			"type": "POST",
			"url": rootPath + '/user/queryUserIntegralLogs',
			// 默认为data，这里定义为demo
			"dataType":'json',
			"contentType": "application/json",
		    "data": function ( data ) {
		    	var s1 = $("#s1").val();
		    	var s2 = $("#s2").val();
		    	if(s1) {
		    		data.columns[0].search.value = $.trim(s1);
		    	} else {
		    		data.columns[0].search.value = "";
		    	}
		    	if(s2) {
		    		data.columns[7].search.value = $.trim(s2);
		    	} else {
		    		data.columns[7].search.value = "";
		    	} 
		    	
		    	var uid = $("#uid").val();
		    	if(uid) {
		    		data.columns[1].search.value = $.trim(uid);
		    	} else {
		    		data.columns[1].search.value = "";
		    	}
		    	var uname = $("#uname").val();
		    	if(uname) {
		    		data.columns[2].search.value = $.trim(uname);
		    	} else {
		    		data.columns[2].search.value = "";
		    	}
		    	var realName = $("#realName").val();
		    	if(realName) {
		    		data.columns[3].search.value = $.trim(realName);
		    	} else {
		    		data.columns[3].search.value = "";
		    	}
		     
		    	var sname = $("#sname").val();
		    	if(sname) {
		    		data.columns[4].search.value = $.trim(sname);
		    	} else {
		    		data.columns[4].search.value = "";
		    	}
		    	var rleIds = "";
		    	$('input:checkbox[name="rleId"]:checked').each(function(){
		    		rleIds += $(this).val() + ",";
		    	});
		    	rleIds = (rleIds != "") ? rleIds.substring(0,rleIds.length - 1) : "";
		    	console.log("rleIds",rleIds)
		    	data.columns[5].search.value = rleIds;
		    	
		    	
		    	var integralTypes = "";
		    	$('input:checkbox[name="integralType"]:checked').each(function(){
		    		integralTypes += $(this).val() + ",";
		    	});
		    	integralTypes = (integralTypes != "") ? integralTypes.substring(0,integralTypes.length - 1) : "";
		    	console.log("integralTypes",integralTypes)
		    	data.columns[6].search.value = integralTypes;
		    	
		    	return JSON.stringify(data);
		    },
		    error:function(){
		    	$("#refresh").click();
		    }
         
		},
		"columns" : [ 
			{
				"name" : "cts",
				"data" : "cts",
				"orderable" : false,
			}
			,{
			"name" : "uid",
			"data" : "uid",
			"orderable" : false,
			}
			,{
			"name" : "uname",
			"data" : null,
			"render" :function(data, type, row, meta) {
						return data.uname?data.uname:"";
					},
			"orderable" : false,
			}
			,{
			"name" : "realName",
			"data" : null,
			"render" :function(data, type, row, meta) {
						return data.realName?data.realName:"";
					},
			"orderable" : false,
			}
			,{
			"name" : "sname",
			"data" : null,
			"render" :function(data, type, row, meta) {
						return data.sname?data.sname:"";
					},
			"orderable" : false,
			}, 
			{
			"name" : "rleNames",
			"data" : null,
			"render" :function(data, type, row, meta) {
				if(data.rleIds == '20181529574197910125') {
					if(data.sid.substr(0,2) == "SQ") {
						return "社区梦想中心";
					} else {
						return "学校梦想中心"
					}
				}
				return data.rleNames?data.rleNames:"";
			},
			"orderable" : false,
		},
		{
			"name" : "itName",
			"data" : "itName",
			"orderable" : false
		},
		{
			"name" : "increaseIntegral",
			"data" : "increaseIntegral",
			"orderable" : false
		},
		{
			"name" : "comment",
			"data" : "comment",
			"orderable" : false
		},
		{
			"name" : "creator",
			"data" : "creator",
			"orderable" : false
		}
		]
	});
	
	var CheckboxDropdown = function(el) {
        var _this = this;
        this.isOpen = false;
        this.areAllChecked = false;
        this.$el = $(el);
        this.$label = this.$el.find('.dropdown-label');
        this.$textLabel = this.$el.find('.text-label');
        this.$checkAll = this.$el.find('[data-toggle="check-all"]').first();
        this.$inputs = this.$el.find('[type="checkbox"]');

        this.onCheckBox();

        this.$label.on('click', function(e) {
            e.preventDefault();
            _this.toggleOpen();
        });

        this.$checkAll.on('click', function(e) {
            e.preventDefault();
            _this.onCheckAll();
        });

        this.$inputs.on('change', function(e) {
            _this.onCheckBox();
        });
    };

    CheckboxDropdown.prototype.onCheckBox = function() {
        this.updateStatus();
    };

    CheckboxDropdown.prototype.updateStatus = function() {
        var checked = this.$el.find(':checked');
        this.areAllChecked = false;
        this.$checkAll.html('全选');
        if (checked.length <= 0) {
            this.$label.html(this.$textLabel.text());
        } else if (checked.length === this.$inputs.length) {
        	this.$label.html(checked.parent('label').text());
            this.areAllChecked = true;
            this.$checkAll.html('全不选');
        } else {
        	this.$label.html(checked.parent('label').text());
        }
    };

    CheckboxDropdown.prototype.onCheckAll = function(checkAll) {
        if (!this.areAllChecked || checkAll) {
            this.areAllChecked = true;
            this.$checkAll.html('Uncheck All');
            this.$inputs.prop('checked', true);
        } else {
            this.areAllChecked = false;
            this.$checkAll.html('Check All');
            this.$inputs.prop('checked', false);
        }
        this.updateStatus();
    };

    CheckboxDropdown.prototype.toggleOpen = function(forceOpen) {
        var _this = this;
        if (!this.isOpen || forceOpen) {
            this.isOpen = true;
            this.$el.addClass('on');
            $(document).on('click', function(e) {
                if (!$(e.target).closest('[data-control]').length) {
                    _this.toggleOpen();
                }
            });
        } else {
            this.isOpen = false;
            this.$el.removeClass('on');
            $(document).off('click');
        }
    };

    var checkboxesDropdowns = document.querySelectorAll('[data-control="checkbox-dropdown"]');
    for (var i = 0, length = checkboxesDropdowns.length; i < length; i++) {
        new CheckboxDropdown(checkboxesDropdowns[i]);
    }

	$("#searchuserIntegralLog").click(function() {
		$("#userIntegralLogTable").dataTable().fnClearTable();
	});
	$("#userIntegralLogTable").find('tbody').addClass('tableTR');
	//导出excel报表
	$("#export").click(function() {
		var startTime = $("#s1").val();
    	var endTime = $("#s2").val();
    	var uid = $("#uid").val();
    	var uname = $("#uname").val();
    	var sname = $("#sname").val();
    	var rleIds = "";
    	$('input:checkbox[name="rleId"]:checked').each(function(){
    		rleIds += $(this).val() + ",";
    	});
    	rleIds = (rleIds != "") ? rleIds.substring(0,rleIds.length - 1) : "";
    	var integralTypes = "";
    	$('input:checkbox[name="integralType"]:checked').each(function(){
    		integralTypes += $(this).val() + ",";
    	});
    	integralTypes = (integralTypes != "") ? integralTypes.substring(0,integralTypes.length - 1) : "";
		exportExcel(rootPath + "/user/exportUpdateUserIntegralLogsExcel",{"startTime":startTime,"endTime":endTime,"uid":uid,"uname":uname,"sname":sname,"rleIds":rleIds,"integralTypes":integralTypes});
	});
	 
});



function exportExcel(url, params) {
    // 创建form元素
    var temp_form = document.createElement("form");
    // 设置form属性
    temp_form.action = url;      
    temp_form.target = "_self";
    temp_form.method = "post";      
    temp_form.style.display = "none";
    // 处理需要传递的参数 
    for (var x in params) { 
        var opt = document.createElement("input");      
        opt.name = x;      
        opt.value = params[x];
        temp_form.appendChild(opt);      
    }      
    document.body.appendChild(temp_form);
    // 提交表单      
    temp_form.submit();
}